package com.summer.io;

import org.junit.Test;

import java.io.*;

public class FileReaderWriterTest {
    @Test
    public void testFlieReader(){
        File file = new File("com/summer/io/hello.txt");
        FileReader fileReader = null;
        if (file.exists()){
            try {
               fileReader = new FileReader(file);
                int data = fileReader.read();
                while (data!=-1){
                    System.out.print((char)data);
                    data = fileReader.read();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    if(fileReader!=null)
                        fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }else {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    @Test
    public void testFileReader1() {
        //1.File类的实例化
        File file = new File("com\\summer\\io\\hello.txt");
        //2.FileReader流的实例化
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            //3.读入的操作
            char[] cbuf = new char[5];
            int len;//read(char[] cbuf 返回每次读入字符的个数，如果达到文件末尾返回-1)
            while ((len=fr.read(cbuf))!=-1){
                for (int i = 0; i < len ; i++) {
                    System.out.println(cbuf[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.资源的关闭
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public  void testFileWriter() throws IOException {
        //不存在 创建
        //存在 覆盖
        File file = new File("com\\summer\\io\\hello1.txt");
        System.out.println(file.getAbsolutePath());
        FileWriter fw = new FileWriter(file,true);//存在 追加
        fw.write("I have a dream!\n");
        fw.write("I want to be a man who are the richest in the world!");
        fw.close();
    }
    @Test
    public void teatFileReaderAndWriter(){
        FileReader fr = null;
        FileWriter fw = null;
        try {
            File file = new File("com\\summer\\io\\hello1.txt");
            File destfile = new File("com\\summer\\io\\test.txt");
            fr = new FileReader(file);
            fw = new FileWriter(destfile);
            char[] cbuf = new char[5];
            int len;
            while ((len=fr.read(cbuf))!=-1){
                fw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                try {
                    if (fw!=null)
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            try {
                if (fr!=null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
